<template>
  <div class="goods">
    <van-swipe class="goods-swipe" :autoplay="3000">
      <van-swipe-item v-for="image in spu.showImgList" :key="image">
        <img :src="image" />
      </van-swipe-item>
    </van-swipe>

    <van-cell-group>
      <van-cell>
        <div class="goods-title">{{ spu.title }}</div>
        <div class="goods-price">{{ formatPrice(spu.price) }}</div>
      </van-cell>
    </van-cell-group>

    <van-cell-group class="goods-cell-group">
      <van-cell :title="skuTitle" is-link @click="selectSku" />
      <sku ref="sku" @receiveSku="receiveSku" />
    </van-cell-group>

    <van-cell-group class="goods-cell-group">
      <van-cell title="查看商品详情" is-link @click="sorry" />
    </van-cell-group>

    <van-goods-action>
      <van-goods-action-icon icon="chat-o" @click="sorry">客服</van-goods-action-icon>
      <van-goods-action-icon icon="cart-o" @click="onClickCart">购物车</van-goods-action-icon>
      <van-goods-action-button type="warning" @click="sorry">加入购物车</van-goods-action-button>
      <van-goods-action-button type="danger" @click="sorry">立即购买</van-goods-action-button>
    </van-goods-action>
  </div>
</template>

<script>
import {
  Tag,
  Col,
  Icon,
  Cell,
  CellGroup,
  Swipe,
  Toast,
  SwipeItem,
  GoodsAction,
  GoodsActionIcon,
  GoodsActionButton
} from "vant";
import Sku from "./sku";
import { apiGetSpuVo } from "@/api/product";

export default {
  components: {
    [Tag.name]: Tag,
    [Col.name]: Col,
    [Icon.name]: Icon,
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
    [Swipe.name]: Swipe,
    [SwipeItem.name]: SwipeItem,
    [GoodsAction.name]: GoodsAction,
    [GoodsActionIcon.name]: GoodsActionIcon,
    [GoodsActionButton.name]: GoodsActionButton,
    [Sku.name]: Sku
  },

  data() {
    return {
      skuTitle: "选择商品",
      spu: {
        title: "",
        price: 0,
        showImgList: []
      }
    };
  },

  methods: {
    formatPrice() {
      return "¥" + (this.spu.price / 100).toFixed(2);
    },
    onClickCart() {
      this.$router.push("cart");
    },
    selectSku() {
      this.$refs.sku.selectSku(this.spu);
    },
    sorry() {
      Toast("暂无后续逻辑~");
    },
    receiveSku(value) {
      console.log(value);
      this.skuTitle = value;
    }
  },
  created() {
    const id = this.$route.query.id;
    apiGetSpuVo({ id: id }).then(res => {
      console.log(res.data);
      const spuData = res.data;
      const spu = {
        id: spuData.id,
        title: spuData.title,
        price: spuData.skus[0].price,
        showImgList: spuData.showImgList
      };
      console.log(spu);
      const list = spuData.skus.map(item => {
        const listItem = {
          id: item.id, // skuId，下单时后端需要
          title: item.title,
          price: item.price, // 价格（单位分）
          stock_num: item.stock, // 当前 sku 组合对应的库存
          imgUrl: item.imgUrl
        };
        const keys = item.attrKey.split("_");
        keys.forEach((item, index) => {
          listItem["s" + (index + 1)] = item;
        });
        return listItem;
      });

      const saleAttr = spuData.saleAttr.map(item => {
        return {
          k_s: "s" + item.id,
          k: item.name,
          v: item.options
        };
      });
      this.spu = spu;
      const sku = {
        tree: saleAttr,
        list: list
      };
      console.log(sku);
      this.spu.sku = sku;
    });
  }
};
</script>

<style lang="less">
.goods {
  padding-bottom: 50px;

  &-swipe {
    img {
      width: 100%;
      display: block;
    }
  }

  &-title {
    font-size: 16px;
  }

  &-price {
    color: #f44;
  }

  &-express {
    color: #999;
    font-size: 12px;
    padding: 5px 15px;
  }

  &-cell-group {
    margin: 15px 0;

    .van-cell__value {
      color: #999;
    }
  }

  &-tag {
    margin-left: 5px;
  }
}
</style>
